﻿#include "userrelationnetworkaccessmanager.h"

const static QString USER_RELATION_BASE_URL = BASE_URL+"userRelation/";
const static QString USER_RELATION_QUERY_PLATFORM_USER = USER_RELATION_BASE_URL+"queryPlatformUser";
const static QString USER_RELATION_ADD_PLATFORM_USER = USER_RELATION_BASE_URL+"addPlatformUser";
const static QString USER_RELATION_GET_USER_RELATIONS = USER_RELATION_BASE_URL+"getUserRelations";
const static QString USER_RELATION_ADD_NONPLATFORM_USER = USER_RELATION_BASE_URL+"addNonPlatformUser";
const static QString USER_RELATION_QUERY_USERRELATION_DETAIL = USER_RELATION_BASE_URL+"queryUserRelationDetail";
const static QString USER_RELATION_BUILD_SUB_ACCOUNT = USER_RELATION_BASE_URL+"buildSubAccount";
const static QString USER_RELATION_CHANGE_USERRELATION = USER_RELATION_BASE_URL+"changeUserRelation";
const static QString USER_RELATION_UPDATE_USERRELATION = USER_RELATION_BASE_URL+"updateUserRelation";
const static QString USER_RELATION_UPLOAD_USERRELATION = USER_RELATION_BASE_URL+"uploadUserRelation";
const static QString USER_RELATION_ADJUST_USERRELATION_FINANCE = USER_RELATION_BASE_URL+"adjustUserRelationFinance";
const static QString USER_RELATION_DELETE_USERRELATION = USER_RELATION_BASE_URL+"deleteUserRelation";
UserRelationNetworkAccessManager::UserRelationNetworkAccessManager(QObject *parent) : BaseNetworkAccessManager(parent)
{

}

void UserRelationNetworkAccessManager::queryPlatformUser(QString username, QString companyId)
{
    QByteArray data;
    data.append("username="+username+"&");
    data.append("companyId="+companyId);

    doPost(USER_RELATION_QUERY_PLATFORM_USER, data);
}

void UserRelationNetworkAccessManager::addPlatformUser(
        QString sourceUsername,
        QString targetUsername,
        UserRelation::RelationType type)
{
    QByteArray data;
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("type="+QString("%1").arg(type));

    doPost(USER_RELATION_ADD_PLATFORM_USER, data);
}

void UserRelationNetworkAccessManager::getUserRelations(QString username, UserRelation::RelationType type)
{
    QByteArray data;
    data.append("username="+username+"&");
    data.append("type="+QString("%1").arg(type));

    doPost(USER_RELATION_GET_USER_RELATIONS, data);
}

void UserRelationNetworkAccessManager::addNonPlatformUser(
        QString username,
        UserRelation::RelationType type,
        QString nickName,
        QString contact,
        QString tel,
        QString remark,
        QString primaryDirectory,
        QString secondaryDirectory)
{
    QByteArray data;
    data.append("username="+username+"&");
    data.append("type="+QString("%1").arg(type)+"&");
    data.append("nickName="+nickName+"&");
    data.append("contact="+contact+"&");
    data.append("tel="+tel+"&");
    data.append("remark="+remark+"&");
    data.append("primaryDirectory="+primaryDirectory+"&");
    data.append("secondaryDirectory="+secondaryDirectory);

    doPost(USER_RELATION_ADD_NONPLATFORM_USER, data);
}

void UserRelationNetworkAccessManager::queryUserRelationDetail(QString username, QString companyId)
{
    QByteArray data;
    data.append("username="+username+"&");
    data.append("companyId="+companyId);

    doPost(USER_RELATION_QUERY_USERRELATION_DETAIL, data);
}

void UserRelationNetworkAccessManager::buildSubAccount(QString sourceUsername, QString targetUsername, UserRelation::RelationType type)
{
    QByteArray data;
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("type="+QString("%1").arg(type));

    doPost(USER_RELATION_BUILD_SUB_ACCOUNT, data);
}

void UserRelationNetworkAccessManager::changeUserRelation(QString sourceUsername, QString targetUsername, UserRelation::RelationType type)
{
    QByteArray data;
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("type="+QString("%1").arg(type));

    doPost(USER_RELATION_CHANGE_USERRELATION, data);
}

void UserRelationNetworkAccessManager::updateUserRelation(
        QString sourceUsername,
        QString targetUsername,
        UserRelation::RelationType type,
        QString urClientName,
        QString urContact,
        QString urTel,
        QString urRemark,
        QString urCredit,
        QString urPrimaryDirectory,
        QString urSecondaryDirectory,
        QString userNickName,
        QString userPosition,
        QString userTel,
        QString userMobile,
        QString userQQ,
        QString userWechat,
        QString companyCompanyName,
        QString companyContact,
        QString companyTel,
        QString companyFax,
        QString companyWebsite,
        QString companyEmail,
        QString companyProvince,
        QString companyCity,
        QString companyDistrict,
        QString companyAddress,
        QString companyAccount,
        QString companyIntroduction,
        QString companyTicketCompanyName,
        QString companyTaxNum,
        QString companyBank,
        QString companyTicketAccount,
        QString companyTicketTel,
        QString companyTicketAddress)
{
    QByteArray data;
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("type="+QString("%1").arg(type)+"&");
    data.append("urClientName="+urClientName+"&");
    data.append("urContact="+urContact+"&");
    data.append("urTel="+urTel+"&");
    data.append("urRemark="+urRemark+"&");
    data.append("urCredit="+urCredit+"&");
    data.append("urPrimaryDirectory="+urPrimaryDirectory+"&");
    data.append("urSecondaryDirectory="+urSecondaryDirectory+"&");
    data.append("userNickName="+userNickName+"&");
    data.append("userPosition="+userPosition+"&");
    data.append("userTel="+userTel+"&");
    data.append("userMobile="+userMobile+"&");
    data.append("userQQ="+userQQ+"&");
    data.append("userWechat="+userWechat+"&");
    data.append("companyCompanyName="+companyCompanyName+"&");
    data.append("companyContact="+companyContact+"&");
    data.append("companyTel="+companyTel+"&");
    data.append("companyFax="+companyFax+"&");
    data.append("companyWebsite="+companyWebsite+"&");
    data.append("companyEmail="+companyEmail+"&");
    data.append("companyProvince="+companyProvince+"&");
    data.append("companyCity="+companyCity+"&");
    data.append("companyDistrict="+companyDistrict+"&");
    data.append("companyAddress="+companyAddress+"&");
    data.append("companyAccount="+companyAccount+"&");
    data.append("companyIntroduction="+companyIntroduction+"&");
    data.append("companyTicketCompanyName="+companyTicketCompanyName+"&");
    data.append("companyTaxNum="+companyTaxNum+"&");
    data.append("companyBank="+companyBank+"&");
    data.append("companyTicketAccount="+companyTicketAccount+"&");
    data.append("companyTicketTel="+companyTicketTel+"&");
    data.append("companyTicketAddress="+companyTicketAddress+"&");

    doPost(USER_RELATION_UPDATE_USERRELATION, data);
}

void UserRelationNetworkAccessManager::uploadUserRelation(QString username, UserRelation::RelationType type, QList<UserRelation*> relations){
    QByteArray data;
    data.append("username="+username+"&");
    data.append("type="+QString("%1").arg(type)+"&");
    data.append("relations="+UserRelation::toJson(relations));

    doPost(USER_RELATION_UPLOAD_USERRELATION, data);
}

void UserRelationNetworkAccessManager::adjustUserRelationFinance(
        QString sourceUsername,
        QString targetUsername,
        QString relatedMessageId,
        int relationType,
        double oldTicketIn,
        double oldTicketOut,
        double oldMoneyIn,
        double oldMoneyOut,
        double ticketIn,
        double ticketOut,
        double moneyIn,
        double moneyOut){
    QByteArray data;
    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("relatedMessageId="+relatedMessageId+"&");
    data.append("relationType="+QString("%1").arg(relationType)+"&");
    data.append("oldTicketIn="+QString("%1").arg(oldTicketIn)+"&");
    data.append("oldTicketOut="+QString("%1").arg(oldTicketOut)+"&");
    data.append("oldMoneyIn="+QString("%1").arg(oldMoneyIn)+"&");
    data.append("oldMoneyOut="+QString("%1").arg(oldMoneyOut)+"&");
    data.append("ticketIn="+QString("%1").arg(ticketIn)+"&");
    data.append("ticketOut="+QString("%1").arg(ticketOut)+"&");
    data.append("moneyIn="+QString("%1").arg(moneyIn)+"&");
    data.append("moneyOut="+QString("%1").arg(moneyOut)+"&");

    doPost(USER_RELATION_ADJUST_USERRELATION_FINANCE, data);
}

void UserRelationNetworkAccessManager::deleteUserRelation(
        QString sourceUsername,
        QString targetUsername,
        int relationType){
    QByteArray data;

    data.append("sourceUsername="+sourceUsername+"&");
    data.append("targetUsername="+targetUsername+"&");
    data.append("relationType="+QString("%1").arg(relationType));

    doPost(USER_RELATION_DELETE_USERRELATION, data);
}
